<template>
	<view id="collection">
		<view style="height: 20rpx;background: #F5F5F5;"></view>
		<!-- 列表循环 -->
		<view class="collection-list" v-if="list.length>=1">
			<view class="collection-item" v-for="(item,i) in list" :key="item.id" @click="bindDetail(item.id)">
				<image :src="IMG_URL+item.img" mode="aspectFill" class="shop-img"></image>
				<view class="collection-item-right">
					<view class="shop-name">{{item.name}}</view>
					<view class="collection-item-bot">
						<view class="shop-price"><text>¥</text>{{item.price}}</view>
						<!-- <icon class="icon-car" @click.stop="bindCar(item.id)"></icon> -->
					</view>
				</view>
			</view>
			<uni-load-more :status="state"></uni-load-more>
		</view>
		<!-- 没有数据 -->
		<view class="no-list" v-if="list.length<1">
			<image src="../../static/images/collection-no.png" mode=""></image>
			<text>暂无收藏</text>
			<button @click="bindIndex">去逛逛吧</button>
		</view>
	</view>
</template>

<script>
	const app=getApp();
	import {colist,addCart} from '@/api/index.js'
	import uniLoadMore from "@/components/uni-load-more/uni-load-more.vue"
	export default {
		components: {
			uniLoadMore
		},
		data() {
			return {
				IMG_URL: app.globalData.IMG_URL, //图片前缀
				openid:'',
				page:1,
				list: [],
				total: 1, // 总页数
				state: 'more', //加载显示
			}
		},
		onLoad(options) {
			this.openid = uni.getStorageSync('openid');
			this.colist();
		},
		methods: {
			// 获取列表
			colist:function(){
				colist({
					openid:this.openid,
					page:this.page
				}).then(res=>{
					if (res.pagenum == res.total || res.total == 0) {
						this.state = 'noMore'
					} else {
						this.state = 'more'
					}
					this.total = res.total;
					this.list = this.list.concat(res.list);
				})
			},
			
			// 跳转详情
			bindDetail:function(id){
				uni.navigateTo({
					url:`/pageA/goodDetail/goodDetail?id=${id}`
				})
			},
			
			// 加入购物车
			bindCar:function(id){
				addCart({
					openid: this.openid,
					goods_id: id,
					goods_num: 1
				}).then(res => {
					uni.showToast({
						title: '添加成功!',
						duration: 1000
					})
				})
			},
			
			// 跳转首页
			bindIndex:function(){
				uni.navigateTo({
					url:`/pages/index/index`
				})
			},
		},
		
		onReachBottom(){
			if (this.total == this.page) {
				return false;
			}
			this.page += 1;
			this.colist();
		}
	}
</script>

<style lang="scss" scoped>
	.collection-list {
		padding: 0 32rpx;

		.collection-item {
			padding: 28rpx 0 36rpx;
			border-bottom: 2rpx solid #EFEFEF;
			display: flex;
			
			.shop-img {
				width: 188rpx;
				height: 188rpx;
				margin-right: 32rpx;
				border-radius: 10rpx;
			}

			.collection-item-right {
				flex: 1;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				.shop-name {
					color: #333333;
					font-size: 32rpx;
					font-weight: 500;
					line-height: 20px;
				}

				.collection-item-bot {
					display: flex;
					align-items: center;

					.shop-price {
						color: #F6240C;
						font-size: 42rpx;
						flex: 1;

						&>text {
							font-size: 28rpx;
						}
					}

					.icon-car {
						width: 52rpx;
						height: 52rpx;
						background: url(../../static/images/collection-car.png) no-repeat;
						background-size: cover;
					}
				}
			}
		}
	}
	
	.no-list{
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-top: 288rpx;
		&>image{
			width: 423rpx;
			height: 266rpx;
			margin-bottom: 36rpx;
		}
		&>text{
			color: #333333;
			font-size: 24rpx;
		}
		&>button{
			width: 360rpx;
			height: 78rpx;
			background: #FFC102;
			border-radius: 39rpx;
			color: #FFFFFF;
			line-height: 78rpx;
			margin-top: 88rpx;
		}
	}
</style>
